package com.vimtec.remote.server;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

import javax.imageio.ImageIO;

import com.vimtec.remote.IRobot;
import com.vimtec.remote.IRobotAdaptor;

public class DeskEventsServer implements Runnable
{

	private int port = 881;

	private Robot robot = null;

	Rectangle size = null;

	IRobot iRobot = null;

	public DeskEventsServer(int port) throws AWTException
	{
		this.port = port;
		this.robot = new Robot();
		iRobot = new IRobotAdaptor(robot);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		size = new Rectangle(0, 0, screenSize.width, screenSize.height);
	}

	public void run()
	{
		ServerSocket sock = null;
		;
		InputStream in = null;
		ObjectInputStream oin = null;
		try
		{
			sock = new ServerSocket();
			sock.bind(new InetSocketAddress(port));
			sock.setSoTimeout(500);

		}

		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		while (RemoteServer.isRunning())
		{
			Socket client = null;
			try
			{
				client = sock.accept();
				in = client.getInputStream();
				oin = new ObjectInputStream(in);
			} catch (SocketTimeoutException se)
			{
				continue;
			} catch (IOException e)
			{
				e.printStackTrace();
			}

			try
			{
				try
				{
					while (RemoteServer.isRunning())
					{
						// read the method names
						String methodName = (String) oin.readObject();
						
						// read the method parameter types
						Class[] paramTypes = (Class[]) oin.readObject();
						
						//read the arguments
						Object[] args = (Object[]) oin.readObject();
						
						//get the method object using reflection
						Method method = iRobot.getClass().getMethod(methodName,
								paramTypes);
						
						//finally invoke the method on the iRobot instance
						method.invoke(iRobot, args);
					}
				} catch (ClassNotFoundException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		try
		{
			sock.close();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
